package com.archive.api;

import com.alibaba.druid.util.StringUtils;
import com.archive.bean.LaboratoryArchive;
import com.utils.JdbcUtils;
import com.utils.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 查询实验档案
 */
@WebServlet(name = "QueryLaboratoryArchive", urlPatterns = {"/QueryLaboratoryArchive"})
public class QueryLaboratoryArchive extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileNumber = req.getParameter("fileNumber");
        String fileTitle = req.getParameter("fileTitle");
        String animalId = req.getParameter("animalId");
        Connection con =  JdbcUtils.getCon();
        Statement stat =null;
        ResultSet rs = null;
        try{
            stat = con.createStatement();
            String sql = "select * from animalfile09_laboratoryarchive where 1 = 1";
            if (!StringUtils.isEmpty(fileNumber)) {
                sql += (" and FileNumber = '" + fileNumber + "'");
            }
            if (!StringUtils.isEmpty(fileTitle)) {
                sql += (" and FileTitle = '" + fileTitle + "'");
            }
            if (!StringUtils.isEmpty(animalId)) {
                sql += (" and AnimalID = '" + animalId + "'");
            }
            sql += ";";
            System.out.println(sql);
            rs = stat.executeQuery(sql);
            List<LaboratoryArchive> dailyArchiveList = getLaboratoryArchiveList(rs);
            resp.setContentType("application/json;charset=utf-8");
            String json = JsonUtil.obj2Json(dailyArchiveList);
            resp.getWriter().println(json);
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("操作错误！！！");
        } finally {
            JdbcUtils.close3(rs, con, stat);
        }
    }

    /**
     * 获取实验档案列表
     * @param rs
     * @return
     */
    private List<LaboratoryArchive> getLaboratoryArchiveList(ResultSet rs) throws SQLException {
        List<LaboratoryArchive> resultList = new ArrayList<LaboratoryArchive>();
        while (rs.next()) {
            String fileNumber = rs.getString("FileNumber");
            String fileTitle = rs.getString("FileTitle");
            Date fillingDate = rs.getDate("FillingDate");
            Date experimentDate = rs.getDate("ExperimentDate");
            String userID = rs.getString("UserID");
            String animalID = rs.getString("AnimalID");
            String fileState = rs.getString("FileState");
            String experimentDataTip = rs.getString("ExperimentDataTip");
            String file = rs.getString("FileName");
            LaboratoryArchive archive = new LaboratoryArchive();
            archive.setFileNumber(fileNumber);
            archive.setFileTitle(fileTitle);
            archive.setFillingDate(fillingDate);
            archive.setExperimentDate(experimentDate);
            archive.setUserId(userID);
            archive.setAnimalId(animalID);
            archive.setFileState(fileState);
            archive.setExperimentDataTip(experimentDataTip);
            archive.setFile(file);
            resultList.add(archive);
        }
        return resultList;
    }
}
